/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.poli.utilidades;

import com.mysql.jdbc.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Andres Felipe Escobar Lopez
 */
public class ConectorBD {

    private Connection conexion;

    public Connection getConexion() {
        ConexionDB();
        return conexion;
    }

    public void setConexion(Connection conexion) {
        this.conexion = conexion;
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();

        } catch (ClassNotFoundException e1) {
            System.out.println("ClassNotFoundException " + e1.getMessage());
        } catch (InstantiationException e2) {
            System.out.println("InstantiationException " + e2.getMessage());
        } catch (IllegalAccessException e3) {
            System.out.println("IllegalAccessException " + e3.getMessage());
        } catch (Exception e) {
            System.out.println("Exception " + e.getMessage());
        }
    }

    /**
     * Establecemos conexion con la base de datos
     *
     * @author Andres Felipe Escobar Lopez
     */
    public void ConexionDB() {
        try {
            setConexion(DriverManager.getConnection("jdbc:mysql://localhost:3306/mantener", "root", ""));

        } catch (SQLException e4) {
            System.out.println("SQLException " + e4.getMessage());
        } catch (Exception e5) {
            System.out.println("Excepcion  " + e5.getMessage());
        }
    }

    public static void main(String param[]) {
        ConectorBD conection = new ConectorBD();
        try {
            // invocamos el procedimiento almacenado PCN_CONSULTAR
            CallableStatement call;
            call = (CallableStatement) conection.getConexion().prepareCall("CALL PCN_CONSULTAR(?)");
            call.setString(1, "03"); //le pasamos el parametro que necsita  ?
            call.execute();
            ResultSet r = (ResultSet) call.getResultSet();

            if (r != null && !r.next()) {
                while (r.next()) {
                    System.out.println("id:  " + r.getString("USERCOD") + " nombres: " + r.getString("USER"));
                }
            } else {
                System.out.println("no hay datos");
            }
        } catch (Exception e) {
            System.out.println("Excepcion " + e.getMessage());
        }
    }

}
